//
//  UIImage+Extension.swift
//  Blog
//
//  Created by whitcast on 16/7/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

extension UIImage {
    class func getScreenShot() -> UIImage {
        //获取主window
        let window = UIApplication.sharedApplication().keyWindow!
        // 开启上下文
        UIGraphicsBeginImageContext(window.size)
        // 重上下文中获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭上下文
        UIGraphicsEndImageContext()
        return image
    }
    
    // 压缩图片
    func scaleImageWithScaleWidth(scaleWidth: CGFloat) -> UIImage {
        
        //  根据压缩后的宽度计算压缩后的高度
        let scaleHeight = scaleWidth / self.size.width * self.size.height
        
        //  压缩后的大小
        let size = CGSize(width: scaleWidth, height: scaleHeight)
        //  开启上下文
        UIGraphicsBeginImageContext(size)
        
        //  把图片渲染到指定范围内
        self.drawInRect(CGRect(origin: CGPointZero, size: size))
        //  获取上下文中压缩后的图片
        let scaleImage = UIGraphicsGetImageFromCurrentImageContext()
        
        //  关闭上下文
        UIGraphicsEndImageContext()
        
        return scaleImage
        
        
    }

}
